﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Skybound.Gecko;

namespace FrameDisplayServer.Browser
{
    public class MozillaBrowserForm : BrowserForm
    {
        private static bool s_xpcomInitialized = false;

        private GeckoWebBrowser webBrowser;

        public MozillaBrowserForm()
        {
            if (s_xpcomInitialized)
            {
                this.webBrowser = new GeckoWebBrowser();
                this.webBrowser.Dock = DockStyle.Fill;
                this.Controls.Add(this.webBrowser);
            }
            else
            {
                Label label = new Label();
                label.Dock = DockStyle.Fill;
                label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

                label.Text = "To use the Firefox browser engine, you must download xulrunner and extract it so that "
                    + "xpcom.dll resides at 'c:\\xulrunner\\xpcom.dll'.";

                this.Controls.Add(label);
            }
        }

        internal static void NotifyXpcomInitialized()
        {
            s_xpcomInitialized = true;
        }

        public override void Navigate(string url)
        {
            if (this.webBrowser != null)
            {
                this.webBrowser.BeginInvoke(new Action<string>(InternalNavigate), new object[] { url });
            }
        }

        private void InternalNavigate(string url)
        {
            this.ShowWindow();
            if (this.webBrowser != null)
            {
                this.webBrowser.Navigate(url);
            }
        }
    }
}
